<?php
/**
 * Created by PhpStorm.
 * User: jimmyhsu
 * Date: 2016/10/3
 * Time: 上午1:21
 */
include ("conn.php");
$name=str_replace(" ","",$_POST['name']);
$password=md5($_POST['password']);
$mobile=$_POST['mobile'];
$sex=$_POST['sex'];
$stuId=$_POST['stuId'];
$age=$_POST['age'];
$regdate=time() * 1000;
$deviceId=$_POST['deviceId'];
$sql="select * from userinfo where name = '$name'";
$result=mysql_query($sql);
if (mysql_num_rows($result) > 0) {
    echo "name already exists";
    return;
}
$sql="select * from userinfo where mobile = '$mobile'";
$result=mysql_query($sql);
if (mysql_num_rows($result) > 0) {
    echo "mobile already exists";
    die();
}
$sql="insert into userinfo (id, name, password, sex, studentid, mobile, signature, age, image, regdate, deviceid) values (NULL, '$name', '$password', $sex, '$stuId', '$mobile', '', $age, '', $regdate, '$deviceId')";
if(!mysql_query($sql)){
    die("register fail");
}
$insertId = mysql_insert_id();
$uploads_dir = "/Library/WebServer/Documents/phpprojects/userimage";
    
        if ((($_FILES["file"]["type"] == "image/jpeg")
            || ($_FILES["file"]["type"] == "image/jpg")
            || ($_FILES["file"]["type"] == "image/png"
            )))
        {
            if ($_FILES["file"]["error"] > 0)
            {
				mysql_query("delete from userinfo where id=$insertId");
                die("Return Code: " . $_FILES["file"]["error"] . "<br />");
            }
            else
            {
                $name = basename($_FILES["file"]["name"]);
                $extension = pathinfo("$uploads_dir/$name", PATHINFO_EXTENSION);
                move_uploaded_file($_FILES["file"]["tmp_name"],
                    $uploads_dir."/".$insertId.".jpg");
            }
        }
        else
        {
			mysql_query("delete from userinfo where id=$insertId");
            die("Invalid file".$_FILES["file"]["type"]);
        }
    
	$imageUrl = "userimage/".$insertId.".jpg";
    $sql = "update userinfo set image='$imageUrl' where id=$insertId";
    if (!mysql_query($sql)) {
		mysql_query("delete from userinfo where id=$insertId");
		die("fail");
	}
echo "register success";

